<?php
/**
 * @name IndexController
 * @author root
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */

//require_once APPLICATION_PATH.'/application/library/QQMailer.php';
class IndexController extends Yaf_Controller_Abstract {

	/** 
     * 默认动作
     * Yaf支持直接把Yaf_Request_Abstract::getParam()得到的同名参数作为Action的形参
     * 对于如下的例子, 当访问http://yourhost/sample/index/index/index/name/root 的时候, 你就会发现不同
     */
	public function indexAction($name = "Stranger") {
        $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
        $ret = $client->connect("127.0.0.1", 8082);
        if(empty($ret)){
            echo 'error!connect to swoole_server failed';
        } else {
            $client->send('blue');//这里只是简单的实现了发送的内容
        }
        return false;
	}

    public function loginAction()
    {
        if ($this->getRequest()->getMethod()=='POST'){
            require_once 'public/Client.php';
            $client = new Client();

            //$user = new UserModel();
            //$row = $user->_select();
            $params = [
                [
                    'to'        => '2311272262@qq.com',
                    'subject'   => '123',
                    'content'   => '888',
                    'headers'   => 'From: webmaster@example.com'
                ],
                [
                    'to'        => '1527093040@qq.com',
                    'subject'   => '123',
                    'content'   => '888',
                    'headers'   => 'From: webmaster@example.com'
                ],

            ];
            $msg = json_encode($params,true);
            $client->send($msg);
            echo "[".date("Y-m-d H:i:s")."]继续忙其他的".PHP_EOL;
            //$row = $user->_select();
        }


    }

    public function testAction()
    {
        if ($this->getRequest()->getMethod()=='post'){

        }else{
            $mailer = new QQMailer();
            $to  = 'chl18756963109@sina.com';
            $title = '愿得一人心，白首不相离。';
            $content = <<< EOF
                <p align="center">
                皑如山上雪，皎若云间月。<br>
                闻君有两意，故来相决绝。<br>
                今日斗酒会，明旦沟水头。<br>
                躞蹀御沟上，沟水东西流。<br>
                凄凄复凄凄，嫁娶不须啼。<br>
                愿得一人心，白首不相离。<br>
                竹竿何袅袅，鱼尾何簁簁！<br>
                男儿重意气，何用钱刀为！</p>
EOF;

            // 发送QQ邮件
            $res = $mailer->send($to, $title, $content);
            var_dump($res);
        }
    }

    public function syncSendAction(){
        $serv = new swoole_client('0.0.0.0',8082);

        $serv->set(array('task_worker_num' => 4));
        $serv->on('receive', function($serv, $fd, $from_id, $data) {
            $task_id = $serv->task($data);
            echo "开始投递异步任务 id=$task_id\n";
        });
        $serv->on('task', function ($serv, $task_id, $from_id, $data) {
            echo "接收异步任务[id=$task_id]".PHP_EOL;
            $mailer = new QQMailer();
            for ($i = 0 ; $i<20;$i++){
                if ( $mailer->send('937069176@qq.com', 'test'.$i, $data)){
                    echo 'send'.$i.' success'."\n";
                }else{
                    echo 'send'.$i.' fail'."\n";
                }
            }
            $serv->finish('');
        });
        $serv->on('finish', function ($serv, $task_id, $data) {
            echo "异步任务[id=$task_id]完成".PHP_EOL;
        });
        $serv->start();
        return false;
    }

    public function swooleAction()
    {
        $params = [
            [
                'to'        => 'chl18756963109@sina.com',
                'title'   => '123',
                'content'   => '888',
                'headers'   => 'From: 1527093040@qq.com'
            ],
            [
                'to'        => 'chl18756963109@sina.com',
                'title'   => '123',
                'content'   => '888',
                'headers'   => 'From: 1527093040@qq.com'
            ],

        ];
        $msg = json_encode($params,true);
        echo "[".date("Y-m-d H:i:s")."]继续忙其他的".PHP_EOL;

        $client = new swoole_client(SWOOLE_SOCK_TCP);
        //连接到服务器
        if (!$client->connect('127.0.0.1', 9501, 0.5))
        {
            $this->write("connect failed.");
        }
        //向服务器发送数据
        if (!$client->send($msg))
        {
            $this->write("send ".$msg." failed.");
        }
        //关闭连接
        $client->close();
        return false;
    }
}
